package com.wn.service.doctorService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.PageParam;
import com.wn.mapper.MedicalAdviceMapper;
import com.wn.ov.MedicalAdviceOv;
import com.wn.ov.PatientOv;
import com.wn.pojo.MedicalAdvice;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 小许
 * @Date 2020/10/26 10:46
 */
@Service
public class MedicalAdviceService {
    @Autowired
    private MedicalAdviceMapper medicalAdviceMapper;

    public ServerResponse addMedicalAdvice(MedicalAdvice medicalAdvice) {
        ServerResponse serverResponse = new ServerResponse();
        int result = medicalAdviceMapper.insertSelective(medicalAdvice);
        if (result > 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "添加成功");
        }
        return new ServerResponse(ResponseCode.ERROR, "添加失败");
    }


    public ServerResponse updateMedicalAdvice(MedicalAdvice medicalAdvice) {
        ServerResponse serverResponse = new ServerResponse();
        int result = medicalAdviceMapper.updateByPrimaryKeySelective(medicalAdvice);
        if (result > 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "修改成功");
        }
        return new ServerResponse(ResponseCode.ERROR, "修改失败");
    }

    /**
     * 获取医嘱
     */
    public ServerResponse getMedicalAdvice(Map<String, Object> map) {
        //map集合取出对象
        PageParam<MedicalAdvice> pageParam = null;
        PatientOv patientOv = null;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if ("patientOv".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                patientOv = (PatientOv) JSONObject.toBean(jsonObject, PatientOv.class); //将json转成需要的对象
            } else if ("pageParam".equals(entry.getKey())) {
                JSONObject jsonObject = JSONObject.fromObject(entry.getValue()); // 将数据转成json字符串
                pageParam = (PageParam<MedicalAdvice>) JSONObject.toBean(jsonObject, PageParam.class); //将json转成需要的对象
            }
        }
        if (StringUtils.isEmpty(patientOv)) {
            return new ServerResponse(ResponseCode.ERROR, "请先选择患者");
        }

        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }

            List<MedicalAdviceOv> ovList = medicalAdviceMapper.superSearch(pageParam.getSuperSearchKeyWord(), patientOv.getId());
            for (MedicalAdviceOv medicalAdviceOv : ovList) {

                if (medicalAdviceOv.getStatus() == 0) {
                    medicalAdviceOv.setStatu("未执行");
                } else if (medicalAdviceOv.getStatus() == 1) {
                    medicalAdviceOv.setStatu("已执行");
                }
            }
            PageInfo<MedicalAdviceOv> pageInfo = new PageInfo<MedicalAdviceOv>(ovList);
            return new ServerResponse(ResponseCode.SUCCESS, pageInfo, "查找成功");

        } else {
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<MedicalAdviceOv> medicalAdviceList = medicalAdviceMapper.selectByPatientId(patientOv.getId());
            for (MedicalAdviceOv medicalAdviceOv : medicalAdviceList) {

                if (medicalAdviceOv.getStatus() == 0) {
                    medicalAdviceOv.setStatu("未执行");
                } else if (medicalAdviceOv.getStatus() == 1) {
                    medicalAdviceOv.setStatu("已执行");
                }
            }
            PageInfo<MedicalAdviceOv> pageInfo = new PageInfo(medicalAdviceList);
            return new ServerResponse(ResponseCode.SUCCESS, pageInfo, "查找成功");
        }
    }

    public ServerResponse deleteMedicalAdvice(MedicalAdvice medicalAdvice) {
        ServerResponse serverResponse = new ServerResponse();
        int result = medicalAdviceMapper.deleteByPrimaryKey(medicalAdvice.getId());
        if (result > 0) {
            return new ServerResponse(ResponseCode.SUCCESS, "删除成功");
        }
        return new ServerResponse(ResponseCode.ERROR, "删除失败");
    }

    //执行医嘱
    public ServerResponse doMedicalAdvice(Integer id,Integer userId) {
        int result = medicalAdviceMapper.updateStatusById(id,userId);
        if (result > 0) {
            //执行完毕

            return new ServerResponse(ResponseCode.SUCCESS, "执行成功");
        }
        //操作失败
        return new ServerResponse(ResponseCode.ERROR, "操作失败");
    }

}
